package com.jing.weibo.ws.sina.utils;

import org.apache.log4j.Logger;

import com.jing.system.comm.contants.SystemCons;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;

public class OauthUtil {

	private static final Logger logger = Logger.getLogger(OauthUtil.class);

	/**
	 * 根据code获取用户授权的信息
	 * @param code
	 * @param oauth
	 * @return
	 * @throws WeiboException
	 */
	public static AccessToken getAccessTokenByCode(String code, Oauth oauth) throws WeiboException {
		// TODO Auto-generated method stub
		AccessToken accessToken = null;
		try {
			accessToken = oauth.getAccessTokenByCode(code);
		} catch (Exception e) {
			logger.error("根据code[" + code + "]获取用户授权的信息异常, 等待" + SystemCons.THREAD_SLEEP_DATE + "ms后系统进行第二次获取!", e);
			try {
				Thread.sleep(SystemCons.THREAD_SLEEP_DATE);
			} catch (InterruptedException e1) {
				logger.error("等待" + SystemCons.THREAD_SLEEP_DATE + "ms异常!", e1);
			}
			accessToken = oauth.getAccessTokenByCode(code);
		}
		return accessToken;
	}

}
